package com.example.statemachine.event;

import com.example.statemachine.StatesEvents;
import com.example.statemachine.action.StartToNewAction;
import com.example.statemachine.action.UploadDocAction;
import com.example.statemachine.guard.StartToNewGuard;
import com.example.statemachine.guard.UploadGuard;
import lombok.AllArgsConstructor;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.stereotype.Component;

/**
 * @Author: leizj
 * @Desc:
 */
@Component
@AllArgsConstructor
public class UploadEvent implements StateMachineEvent{
    private UploadGuard uploadGuard;
    private UploadDocAction uploadDocAction;

    @Override
    public void configure(StateMachineTransitionConfigurer<StatesEvents.State, StatesEvents.Event> transitions) throws Exception {
        transitions.withExternal()
                .source(StatesEvents.State.START)
                .event(StatesEvents.Event.UPLOAD_DOC)
                .target(StatesEvents.State.PENDING_DOC_CHOICE)
                .action(uploadDocAction)
                .and()
                .withChoice()
                .source(StatesEvents.State.PENDING_DOC_CHOICE)
                .first(StatesEvents.State.DOCUMENT_UPLOADED,uploadGuard)
                .last(StatesEvents.State.PENDING_DOC)

                .and()
                .withExternal()
                .source(StatesEvents.State.PENDING_DOC)
                .event(StatesEvents.Event.UPLOAD_DOC)
                .target(StatesEvents.State.PENDING_DOC_CHOICE)
                .action(uploadDocAction)
                .and()
                .withChoice()
                .source(StatesEvents.State.PENDING_DOC_CHOICE)
                .first(StatesEvents.State.DOCUMENT_UPLOADED,uploadGuard)
                .last(StatesEvents.State.PENDING_DOC)

                .and()
                .withExternal()
                .source(StatesEvents.State.DOCUMENT_UPLOADED)
                .event(StatesEvents.Event.UPLOAD_DOC)
                .target(StatesEvents.State.PENDING_DOC_CHOICE)
                .action(uploadDocAction)
                .and()
                .withChoice()
                .source(StatesEvents.State.PENDING_DOC_CHOICE)
                .first(StatesEvents.State.DOCUMENT_UPLOADED,uploadGuard)
                .last(StatesEvents.State.PENDING_DOC);
    }
}
